<template>
  <div class="login_contrainer">
    <div class="login_content">
      <div class="login_title"><img src="../../assets/logo.png" alt="" /></div>
      <div class="login_form">
        <el-form
          :model="param"
          :rules="rules"
          ref="login"
          label-width="0px"
          class="ms-content"
        >
          <el-form-item prop="mobile">
            <el-input v-model="param.mobile" placeholder="电话号码">
              <el-button slot="prepend" icon="el-icon-user"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="password"
              v-model="param.password"
              @keyup.enter.native="submitForm()"
            >
              <el-button slot="prepend" icon="el-icon-lock"></el-button>
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button
              type="primary"
              @click="submitForm()"
              style="
                letter-spacing: 1em;
                font-size: 17px;
                height: 80%;
                line-height: 80%;
              "
              >登录</el-button
            >
          </div>
          <!-- <el-button type="text" style="font-size: 16px"
            ><a
              class="register-btn"
              href="#"
              target="newwindow"
              style="float: left; text-decoration: none"
              >管理员</a
            ></el-button
          > -->
          <el-button
            type="text"
            @click="register"
            class="register-btn"
            style="font-size: 16px"
            >去注册</el-button
          >
        </el-form>

      </div>
    </div>
  </div>
</template>

<script>
import { login } from "../../api/login";
import { getUserId } from "../../api/user";
export default {
  name: "Login",
  data() {
    return {
      Verification: true,
      timer: 60,
      param: {
        mobile: "",
        password: "",
      },
      rules: {
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
    };
  },
  created() {},
  methods: {
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          login(this.param.mobile, this.param.password).then((res) => {
            if (res.data.code == 0) {
              this.$message.success("登录成功");
              // localStorage.setItem("u_id", 1);
              sessionStorage.setItem("phoneNum", this.param.mobile);
              localStorage.setItem("token", res.data.token);
              getUserId().then((res) => {
                this.$router.push("/");
                localStorage.setItem("u_id", res.data.userId);
              });
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("请按要求输入!");
          return false;
        }
      });
    },
    register() {
      this.$router.push("/register");
    },
    cancel() {
      this.$refs["registerForm"].resetFields();
      this.dialogFormVisible = false;
    },
  },
};
</script>

<style scoped>
.login_contrainer {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.login_content {
  width: 40%;
}
.login_title {
  text-align: center;
  background-color: rgba(240, 248, 255, 0.5);
}
.login_title img {
  width: 100%;
}
.login_form {
  width: 100%;
  background-color: rgba(240, 248, 255, 0.5);
}
.login_contrainer {
  width: 100%;
  height: 100vh;
  background-image: url("../../assets/login.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-position: center;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
  line-height: 18px;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
.register-btn {
  float: right;
}
</style>